import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import form.CodeUnit;
import form.GeneratingUnitFilesDialog;
import form.Utils;
import org.jetbrains.annotations.NotNull;

import java.text.DecimalFormat;

/**
 * 生成单位文件
 */
public class GeneratingUnitAction extends AnAction {
    private Project project; // 项目

    @Override
    public void actionPerformed(AnActionEvent e) {
        project = e.getData(PlatformDataKeys.PROJECT);
        // 初始化项目Dialog
        initDialog();
        refreshProject(e);

    }

    /**
     * 刷新项目
     */
    private void refreshProject(@NotNull AnActionEvent e) {
        e.getProject().getBaseDir().refresh(false, true);
    }

    /**
     * 初始化项目Dialog
     */
    private void initDialog() {
        GeneratingUnitFilesDialog dialog = new GeneratingUnitFilesDialog((unit, multiple, maxValue) -> {
            String content = createFile(unit, multiple, maxValue);
            writeToFile(content, project.getBasePath(), "dimens.xml");
            Messages.showInfoMessage(project, "Generate unit file success", "Success");
        });
        dialog.pack();
        dialog.setVisible(true);
    }

    private String createFile(CodeUnit unit, Double multiple, Double maxValue) {
        String unitValue = "mm";
        switch (unit) {
            case MM:
                unitValue = "mm";
                break;
            case DP:
                unitValue = "dp";
                break;
            case IN:
                unitValue = "in";
                break;
            case PT:
                unitValue = "pt";
                break;
        }
        DecimalFormat format = new DecimalFormat("##.##");
        StringBuilder content = new StringBuilder();
        content.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        content.append("\n");
        content.append("<resources xmlns:tools=\"http://schemas.android.com/tools\" tools:ignore=\"InOrMmUsage\">");
        double currentValue = 0.0d;
        while (currentValue < maxValue) {
            content.append("\n").append("\t");
            content.append("<dimen name=\"").append(unitValue).append("_").append(getValue(format, currentValue)).append("\">").append(currentValue).append(unitValue).append("</dimen>");
            // currentValue += multiple;
            currentValue = Utils.addDouble(currentValue, multiple);
        }
        content.append("\n");
        content.append("\t");
        content.append("<dimen name=\"").append(unitValue).append("_").append(getValue(format, maxValue)).append("\">").append(maxValue).append(unitValue).append("</dimen>");
        content.append("\n");
        content.append("</resources>");
        return content.toString();
    }

    private String getValue(DecimalFormat format, double currentValue) {
        try {
            return format.format(currentValue).replace(".", "_");
        } catch (Exception e) {
            e.printStackTrace();
            return String.valueOf(currentValue).replace(".", "_");
        }
    }

    /**
     * 生成
     *
     * @param content   类中的内容
     * @param classPath 类文件路径
     * @param className 类文件名称
     */
    private void writeToFile(String content, String classPath, String className) {
        Utils.writeToFile(content, classPath, className);
    }
}